//-----------------------------------------------------------------------
// <copyright file="tblConstantDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblConstantDAOBase
    {
        #region Common methods
        public virtual tblConstant CreatetblConstantFromReader(IDataReader reader)
        {
            tblConstant item = new tblConstant();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (int)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"ParameterID")) item.ParameterID = (int)reader["ParameterID"];
                if (UtilityDAO.CheckColumnData(reader,"StringValue")) item.StringValue = (string)reader["StringValue"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblConstantFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblConstant methods
            
        public virtual void CreatetblConstant(tblConstant tblConstant)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblConstantInsert");
                
                database.AddInParameter(dbCommand, "@ParameterID", DbType.Int32, tblConstant.ParameterID);
                database.AddInParameter(dbCommand, "@StringValue", DbType.AnsiString, tblConstant.StringValue);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblConstant.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblConstantException, ex);
            }
        }

        #endregion

        #region UpdatetblConstant methods
        
        public virtual void UpdatetblConstant(tblConstant tblConstant)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblConstantUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, tblConstant.ID);
                database.AddInParameter(dbCommand, "@ParameterID", DbType.Int32, tblConstant.ParameterID);
                database.AddInParameter(dbCommand, "@StringValue", DbType.AnsiString, tblConstant.StringValue);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblConstantException, ex);
            }
        }
        
        #endregion

        #region DeletetblConstant methods
        public virtual void DeletetblConstant(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblConstantDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblConstantException, ex);
            }
        }

        #endregion

        #region GettblConstant methods
        
        public virtual tblConstant GettblConstant(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblConstantGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                tblConstant tblConstant =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblConstant = CreatetblConstantFromReader(reader);
                        reader.Close();
                    }
                }
                return tblConstant;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblConstantException, ex);
            }
        }
        
        #endregion

        #region GettblConstantList methods
        public virtual tblConstantCollection GettblConstantList(tblConstantColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblConstantGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblConstantCollection tblConstantCollection = new tblConstantCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblConstant tblConstant = CreatetblConstantFromReader(reader);
                        tblConstantCollection.Add(tblConstant);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblConstantCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblConstantListException, ex);
            }
        }
        
        public virtual tblConstantCollection GettblConstantList(tblConstantColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblConstantList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}